package com.jiuyun.servlet;

import com.jiuyun.dao.StudentaDao;
import com.jiuyun.dao.impi.StudentaDaoimpl;
import com.jiuyun.entity.Classa;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/banji/*")
public class BanjiServlet extends BaseServlet {
        StudentaDao dao=new StudentaDaoimpl();
    protected void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List list=null;
        try {
            list=dao.banjiAll();
            System.out.println(list);
            request.getSession().setAttribute("list",list);
        } catch (SQLException throwables){
            throwables.printStackTrace();
        }
        //response.getRequestDispatcher("/houtai/banji.jsp").forward(request,response);
        response.sendRedirect("/Eventstar/houtai/banji.jsp");
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("班级添加");
        String name=request.getParameter("name");
        String date=request.getParameter("date");
        String xxname=request.getParameter("xxname");
        int zt=Integer.parseInt(request.getParameter("zt"));
        int bzr=Integer.parseInt(request.getParameter("bzr"));
        Classa str=new Classa(name,date,xxname,zt,bzr);
        int a=0;
        try {
            a= dao.banjiadd(str);
            System.out.println(a);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if(a>0){
            System.out.println("添加成功！");
        }else{
            System.out.println("添加失败！");
        }
        //request.getRequestDispatcher("/banji/find").forward(request,response);
        response.sendRedirect("banji/find");
    }
    protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("班级删除");
        int id=Integer.parseInt(request.getParameter("banjidel"));
        try {
            dao.banjidel(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //request.getRequestDispatcher("/banji/find").forward(request,response);
        response.sendRedirect("banji/find");
    }
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("班级修改");
        String name=request.getParameter("name");
        String date=request.getParameter("date");
        String xxname=request.getParameter("xxname");
        int zt=Integer.parseInt(request.getParameter("zt"));
        int bzr=Integer.parseInt(request.getParameter("bzr"));
        int id=Integer.parseInt(request.getParameter("cid"));
        try {
            dao.updatebanji(name,date,xxname,zt,bzr,id);
            System.out.println("来了");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //request.getRequestDispatcher("/banji/find").forward(request,response);
        response.sendRedirect("banji/find");
    }
    protected void xiugai(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        int gid=Integer.parseInt(request.getParameter("gid"));
        //System.out.println(request.getParameter("gid"));
        List<Classa> list=dao.banjitjAll(gid);
//        for (Classa C: list){
//            int cid=C.getClassid();
//            String name=C.getClassName();
//            String date=C.getClassCreate();
//            String xxname=C.getClassRemark();
//            int zt=C.getClassState();
//            int bzr=C.getBzr();
//
//            request.setAttribute("cid", cid);
//            request.setAttribute("name", name);
//            request.setAttribute("date", date);
//            request.setAttribute("xxname", xxname);
//            request.setAttribute("zt", zt);
//            request.setAttribute("bzr", bzr);
//        }
        HttpSession session=request.getSession();
        session.setAttribute("banjilist",list);
        response.sendRedirect("/Eventstar/houtai/bjxiugai.jsp");
        //request.getRequestDispatcher("/houtai/bjxiugai.jsp").forward(request, response);
    }
}
